import org.python.core.*;
import org.python.util.PythonInterpreter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Test3 {
    public static void main(String[] args) {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.execfile("D:\\Code\\PyCharm-Project\\AiApp\\production\\demo.py");
        // 调用demo.py中的method1方法
        PyFunction func = interpreter.get("predict", PyFunction.class);
        List<String> a = new ArrayList<String>();
        a.add("'你好 孕妇可以用吗'");
        PyObject pyobj = func.__call__(new PyList(a));
        System.out.println("获得方法的返回值 = " + pyobj.toString());

//        PythonInterpreter interpreter = new PythonInterpreter();
//        interpreter.execfile("D:\\Code\\PyCharm-Project\\AiApp\\production\\test.py");
//        // 调用demo.py中的method1方法
//        PyFunction func = interpreter.get("sum", PyFunction.class);
//        PyObject pyobj = func.__call__(new PyInteger(12), new PyInteger(23));
//        System.out.println("获得方法的返回值 = " + pyobj.toString());

//        Process proc;
//        try {
//            proc = Runtime.getRuntime().exec("D:\\Software\\Anaconda3\\envs\\pytorch\\python.exe D:\\test\\production\\demo.py");// 执行py文件
//            //用输入输出流来截取结果
//            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
//            String line = null;
//            while ((line = in.readLine()) != null) {
//                System.out.println(line);
//            }
//            in.close();
//            proc.waitFor();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

    }
}